﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;

namespace MyPaint
{
    public partial class FrmMoreFunctionTo : Form
    {
        private FrmMessageBox frmMessage;
        private FolderBrowserDialog fbd;

        public FrmMoreFunctionTo()
        {
            InitializeComponent();

            SetTextBoxToLinkChangedEvent(this);
            Control_TextChanged(null, null);

            this.btn_SaveImage.Click += Btn_Click;
            this.btn_SelFileSavePath.Click += Btn_Click;
        }

        private void Btn_Click(object sender, EventArgs e)
        {
            try
            {
                var btn = sender as Button;
                switch (btn.Name)
                {
                    case "btn_SaveImage":
                        string dirPath = txt_ImageSavePath.Text.Trim();
                        if (Directory.Exists(dirPath))
                        {
                            string filePath = Path.Combine(dirPath, $"PP.Paint_{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.png");
                            picBox.Image.Save(filePath, ImageFormat.Png);
                            frmMessage = new FrmMessageBox(this.Location, this.Width, $"保存成功");
                            frmMessage.Show();
                            //转换成功，打开文本保存所在目录
                            Process.Start("explorer.exe", dirPath);
                        }
                        break;

                    case "btn_SelFileSavePath":
                        fbd = new FolderBrowserDialog();
                        if (fbd.ShowDialog() == DialogResult.OK)
                        {
                            string path = fbd.SelectedPath;
                            txt_ImageSavePath.Text = path;
                        }
                        break;

                    default:
                        break;
                }

            }
            catch (Exception ex)
            {
                frmMessage = new FrmMessageBox(this.Location, this.Width, $"{ex.Message}", Color.Brown);
                frmMessage.Show();
            }

        }

        #region 递归处理-link事件
        private void SetTextBoxToLinkChangedEvent(Control control)
        {
            switch (control)
            {
                case TextBox _textBox:
                    _textBox.TextChanged += Control_TextChanged;
                    break;
                case ComboBox _comboBox:
                    _comboBox.SelectedIndexChanged += Control_TextChanged;
                    break;
                case CheckBox _checkBox:
                    _checkBox.CheckedChanged += Control_TextChanged;
                    break;
                default:
                    break;
            }

            if (control.HasChildren)
            {
                foreach (Control item in control.Controls)
                {
                    SetTextBoxToLinkChangedEvent(item);
                }
            }
        }
        #endregion

        private void Control_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int height = int.Parse(txt_Height.Text.Trim());
                int width = int.Parse(txt_Width.Text.Trim());
                picBox.Size = new Size(width, height);
                picBox.Image = new Bitmap(width, height);

                var colorStart = ColorTranslator.FromHtml($"#{txt_ColorStart.Text.Trim()}");
                var colorEnd = ColorTranslator.FromHtml($"#{txt_ColorEnd.Text.Trim()}");

                if (chb_PathToDraw.Checked)//径向
                {
                    //上下 or 左右
                    var drawDirection = chb_UpDownDirection.Checked ? LinearGradientMode.Vertical : LinearGradientMode.Horizontal;
                    using (Graphics g = Graphics.FromImage(picBox.Image))
                    {
                        g.SmoothingMode = SmoothingMode.AntiAlias;
                        var lineBrush = new LinearGradientBrush(
                                                                    this.picBox.ClientRectangle,
                                                                    colorStart,
                                                                    colorEnd,
                                                                    drawDirection);
                        g.FillRectangle(lineBrush, this.picBox.ClientRectangle);
                    }
                }
                else
                {
                    if (chb_ColorCenterToRound.Checked)//中心扩散
                    {
                        GraphicsPath graphicsPath = new GraphicsPath();
                        graphicsPath.AddEllipse(this.picBox.ClientRectangle);
                        using (Graphics g = Graphics.FromImage(picBox.Image))
                        {
                            g.FillRectangle(new SolidBrush(colorEnd), this.picBox.ClientRectangle);
                            g.SmoothingMode = SmoothingMode.AntiAlias;
                            var pathBrush = new PathGradientBrush(graphicsPath);
                            pathBrush.CenterPoint = new PointF(width / 2, height / 2);
                            pathBrush.CenterColor = colorStart;//中心颜色
                            pathBrush.SurroundColors = new Color[] { colorEnd };//最外层颜色
                            g.FillPath(pathBrush, graphicsPath);
                        }
                    }
                }

            }
            catch(Exception ex)
            {
                frmMessage = new FrmMessageBox(this.Location, this.Width, $"{ex.Message}", Color.Brown);
                frmMessage.Show();
            }

        }

    }
}
